package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.ResultInfo;
import entity.MemOrigin;
import entity.MemType;
import net.sf.json.JSONObject;
import service.MemOriginService;
import service.impl.MemOriginServiceImpl;

@WebServlet("/memorigin/*")
public class MemOriginServlet extends HttpServlet{

	MemOriginService service = new MemOriginServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String requestURI = req.getRequestURI();
		String methodName = requestURI.substring(requestURI.lastIndexOf("/")+1);
		switch(methodName) {
		case "save": save(req,resp); break;
		case "getAll": getAll(req,resp); break;
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}

	public void getAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<MemOrigin> list = service.getAll();

		ResultInfo result = new ResultInfo();
		if (!list.isEmpty()) {
			result.setCode(200);
			result.setMessage("查询成功");
			result.setData(list);
		}else {
			result.setCode(10001);
			result.setMessage("查询失败");
		}
		//4.得到输出流
		resp.setContentType("application/json;charset=utf-8");
		PrintWriter out = resp.getWriter();
		out.write(JSONObject.fromObject(result).toString());
		out.flush();
	}
	
	public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}
	
	
}
